<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
</head>
<body class="pear-container">
<!--查询表单-->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" id="select_username" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-inline">
                        <input type="text" name="sex" id="select_sex" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-inline">
                        <input type="text" name="telephonenumber" id="select_telephonenumber" placeholder=""
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="account" id="select_account" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<!--信息展示表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>
<!--添加、修改   组件-->
<div style="display: none" id="showAdd">
    <form class="layui-form" action="" lay-filter="addForm">
        <input type="hidden" name="id" id="update_id">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="account" id="update_account" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" name="username" id="update_username" lay-verify="title"
                                   autocomplete="off" placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="email" id="update_email" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="password" id="update_password" lay-verify="title"
                                   autocomplete="off" placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="telephonenumber" id="update_telephonenumber" lay-verify="title"
                                   autocomplete="off" placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-block" name="sex" id="update_sex" type="hidden">
                            <input type="radio" name="sex" value="1" title="男" checked="">
                            <input type="radio" name="sex" value="0" title="女">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">权限</label>
                        <div class="layui-input-block">
                            <select id="update_valid" name="valid" lay-filter="business">
                                <option value=""></option>
<!--                                <option value="1"> 普通用户</option>-->
<!--                                <option value="2"> 普通管理员</option>-->
<!--                                <option value="3"> 超级管理员</option>-->
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="user-submit">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<!--新增 ，批量删除按钮组件-->
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<!--修改，删除按钮-->
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>
<!--启用禁用组件-->
<script type="text/html" id="user-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{d.enable==1?'checked':''}}>
</script>
<!--时间格式化组件-->
<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.birthday, 'yyyy-MM-dd')}}
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laypage = layui.laypage;

        let MODULE_PATH = "operate/";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '序号',
                    field: 'id',
                    align: 'center',
                    width: 100
                },
                {
                    title: '账号',
                    field: 'account',
                    align: 'center',
                    width: 100
                },
                {
                    title: '用户名',
                    field: 'username',
                    align: 'center'
                },
                {
                    title: '性别',
                    field: 'sex',
                    align: 'center',
                    width: 80,
                    templet: usersex
                },
                {
                    title: '电话',
                    field: 'telephonenumber',
                    align: 'center'
                },
                {
                    title: '启用',
                    field: 'enable',
                    align: 'center',
                    templet: '#user-enable'
                },

                {
                    title: '注册日期',
                    field: 'birthday',
                    align: 'center',
                    templet: '#user-createTime',
                    sort: true
                },
                {
                    title: '备注',
                    field: 'comment',
                    align: 'center'
                    //   templet: '#user-createTime'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        function usersex(d) {
            let str;
            if (d.sex == 1) {
                str = '男';
            } else if (d.sex == 0) {
                str = '女';
            } else {
                str = '<span class="fontColor_red">未知</span>';
            }
            return str;
        }

        table.render({
            elem: '#user-table',
            url: 'http://47.93.226.229/getAllUser',
            page: true,
            limit: 5,
            limits: [5, 10, 15, 20],
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });
        // form.on('select(business)', function (data) {
        $.ajax({
            type: "GET",
            url: 'http://47.93.226.229/selectAuthorityBywhere',  //从数据库查询返回的是个list
            dataType: "json",
            success: function (data) {
                console.log(data);
                $.each(data.data, function (index, item) {
                    $('#update_valid').append(new Option(item.authority, item.roleId));//往下拉菜单里添加元素
                })
                layui.form.render("select");//菜单渲染 把内容加载进去
            }

        });
        // return false;
        // });



//多条件查询
        form.on('submit(user-query)', function (data) {
            if (data.field.sex == '女') {
				data.field.sex = 0;
            } if(data.field.sex == '男') {
				data.field.sex = 1;
            }
            data.field.page=1;
            data.field.limit=5;
            $.ajax({
                url: 'http://47.93.226.229/getAllUser',
                data: data.field,//data，规定要发送服务器的数据
                type: "GET",//规定请求的类型
                dataType: "json",//预期服务器相应的类型
                success: function (a) {
                    table.reload('user-table', {
                        where: {
                            username: $("#select_username").val(),
                            sex: data.field.sex,
                            telephonenumber: $("#select_telephonenumber").val(),
                            account: $("#select_account").val(),
                        } , page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    })
                }
            })
            return false;
        });

        form.on('switch(user-enable)', function (obj) {
            let  enable;
            if (obj.elem.checked){
                enable=1
            }else {
                enable=0
            }
            $.ajax({
                url:'http://47.93.226.229/updateenable',
                data:{"enable":enable,"id":obj.elem.value},
                type:'POST',
                success:function () {

                    table.reload("user-table");
                }
            })
        });
        let index = 0;
        window.add = function () {
            // location.reload();
            // table.reload("user-table");

                let index = 0;
            table.init("showAdd");
            index = layer.open({

                type: 1,
                title: '新增用户',
                area: ['500px', '600px'],
                content: $('#showAdd')
            });

            //表单提交
            form.on('submit(user-submit)', function (data) {
                //校验数据
                //提交数据
                $.post(
                    'http://47.93.226.229/addUser',
                    data.field,
                    function (msg) {
                        layer.close(index);//关闭当前页
                        table.reload("user-table");
                    }
                );
                return false;
            });
        }

        window.edit = function (obj) {
            let data = obj.data //获得当前行数据
            $.get(
                'http://47.93.226.229/selectUser' + data.id, function (obj1) {
                    index = layer.open({
                        type: 1,
                        title: '修改用户',
                        area: ['500px', '600px'],
                        content: $('#showAdd'),//注意，如果str是object，那么需要字符拼接。
                    });
                    content: $("#update_id").val(obj1.data.id);
                    content: $("#update_account").val(obj1.data.account);
                    content: $("#update_username").val(obj1.data.username);
                    content: $("#update_email").val(obj1.data.email);
                    content: $("#update_telephonenumber").val(obj1.data.telephonenumber);
                    content: $("#update_password").val(obj1.data.password);
                    $('input[name=sex]').prop('checked', false);
                    if (obj1.data.sex != null) {
                        $('input[name=sex][value=' + obj1.data.sex + ']').prop('checked', true);
                    }
                    content: $("#update_sex").val(obj1.data.sex);
                    content:$("#update_valid").val(obj1.data.valid)
                    form.render();
                });
            form.on('submit(user-submit)', function (data) {
                //提交数据
                $.post(
                    'http://47.93.226.229/updateArticleWithId',
                    data.field,
                    function (msg) {
                        console.log('success');
                        layer.close(index);//关闭当前页
                        table.reload("user-table");
                    }
                );
            });
        }

        window.remove = function (obj) {
            let data = obj.data //获得当前行数据
            let arr = new Array();
            arr.push(data.id);
            layer.confirm('确定要删除该用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: 'http://47.93.226.229/DeleteUserByids',
                    data: {"ids": arr},//data，规定要发送服务器的数据
                    dataType: 'json',
                    traditional: true,
                    type: "GET",//规定请求的类型
                    success: function (result) {
                        layer.close(loading);
                        if (result.data > 1) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 10
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }

                    }

                })
                table.reload("user-table",{
                    done: function (res, curr, count) {
                        if (curr > 1 && res.data.length === 0) {
                            curr = curr - 1;
                            table.reload('user-table', {
                                page: {
                                    curr: curr
                                },
                                where: {
                                    username: $("#select_username").val(),
                                    sex:$("#select_sex").val(),
                                    telephonenumber: $("#select_telephonenumber").val(),
                                    account: $("#select_account").val(),
                                },
                            }, 'data');
                        }
                    }
                });
            });

        }

        window.batchRemove = function (obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            // let checkIds = common.checkField(obj,'userId');
            let arr = new Array();
            for (let i = 0; i < checkStatus.data.length; i++) {
                arr.push(checkStatus.data[i].id);
            }
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: 'http://47.93.226.229/DeleteUserByids',
                    data: {"ids": arr},//data，规定要发送服务器的数据
                    dataType: 'json',
                    traditional: true,
                    type: 'GET',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 10
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 10
                            });
                        }
                    }
                })
                table.reload("user-table");
            });
        }
        window.refresh = function (param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
